/*******************************************************************

    Author: Xiao
    Date: 2022/3/29 17:35

*******************************************************************/
package dao

type Song struct {
	SongId         int `gorm:"primarykey"`
	SongUser       int
	SongMid        int64
	SongName       string
	SongSinger     string
	SongPic        string
	SongLength     int
	SongPlay       int
	SongWeek       int
	SongFav        int
	SongStatus     int
	SongCreatetime int64 `gorm:"autoCreateTime"`
	SongUpdatetime int64 `gorm:"autoUpdateTime"`
}

type SongResp struct {
	Id     int    `json:"id"`
	Mid    int64  `json:"mid"`
	Name   string `json:"name"`
	Pic    string `json:"pic"`
	Singer string `json:"singer"`
	Week   int    `json:"week"`
	Length int    `json:"length"`
	Album  string `json:"album"`
	Played int    `json:"played"`
}

// 获取热门歌曲
func GetHotSongs() ([]SongResp, error) {
	var songRs []SongResp
	result := MDB.Model(&Song{}).Select("count(song_week) as week,song_mid as mid,max(song_id) as id,max(song_pic) as pic,max(song_singer) as singer,max(song_name) as name").Where("song_week > 0").Group("song_mid").Order("week desc").Limit(50).Find(&songRs)
	return songRs, result.Error
}

// 通过用户和音乐id 获取音乐信息
func GetSongByMidAndUser(mid int64, uid int) (Song, error) {
	var s Song
	result := MDB.Where("song_mid = ? and song_user = ?", mid, uid).First(&s)
	return s, result.Error
}

// 通过音乐id 获取音乐信息
func GetSongByMid(mid int64) (Song, error) {
	var s Song
	result := MDB.Where("song_mid = ? ", mid).First(&s)
	return s, result.Error
}

// 创建
func CreateSong(s Song) (Song, error) {
	result := MDB.Omit("song_play", "song_week", "song_fav", "song_status").Create(&s)
	return s, result.Error
}

// 删除
func DeleteSong(mid int64, uid int) error {
	result := MDB.Where("song_mid = ? and song_user = ?", mid, uid).Delete(&Song{})
	return result.Error
}

// 获取音乐列表
func GetSongList(uid, page, perpage int) ([]SongResp, error) {
	var srs []SongResp
	result := MDB.Model(&Song{}).Select("song_mid as mid,song_length as length,song_name as name,song_singer as singer,song_play as played,song_pic as pic").Where("song_user = ?",
		uid).Order("song_updatetime desc,song_play desc,song_id desc").Limit(perpage).Offset((page - 1) * perpage).Find(&srs)
	return srs, result.Error
}

// 获取音乐列表
func GetUserSongList(uid int, isAll bool) ([]SongResp, error) {
	var srs []SongResp
	var err error
	if isAll {
		result := MDB.Model(&Song{}).Select("song_mid as mid,song_length as length,song_name as name,song_singer as singer,song_play as played,song_pic as pic").Where("song_user = ?",
			uid).Order("song_updatetime desc,song_play desc,song_id desc").Find(&srs)
		err = result.Error
	} else {
		result := MDB.Model(&Song{}).Select("song_mid as mid,song_length as length,song_name as name,song_singer as singer,song_play as played,song_pic as pic").Where("song_user = ?",
			uid).Order("song_updatetime desc,song_play desc,song_id desc").Limit(50).Find(&srs)
		err = result.Error
	}
	return srs, err
}

// 通过mid更新音乐信息
func UpdateSongByMid(mid int64, column map[string]interface{}) error {
	result := MDB.Model(&Song{}).Where("song_mid = ?", mid).Updates(column)
	return result.Error
}

// 通过sid更新音乐信息
func UpdateSongBySid(sid int, column map[string]interface{}) error {
	result := MDB.Model(&Song{}).Where("song_id = ?", sid).Updates(column)
	return result.Error
}
